﻿using Ewell.BizComponent.Charge.UI.VM;
using Ewell.BizComponent.Service.Server;
using Ewell.Interface.Accounting.Common.Model;
using Ewell.Interface.Common.Model.Common;
using Ewell.Interface.Proxy.Model;
using Ewell.Interface.ReadCard.Common.Model.Response;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Ewell.BizComponent.Charge.UI
{
    /// <summary>
    /// SearchComponentFrm.xaml 的交互逻辑
    /// </summary>
    public partial class SearchComponentFrm : Page
    {
        public SearchComponentVm vm { get; set; }
        public SearchComponentFrm()
        {
            InitializeComponent();
        }

        public SearchComponentFrm(SearchComponentVm vm):this() 
        {
            this.vm = vm;
            this.DataContext = vm;
        }

        private void ToggleButton_Click(object sender, RoutedEventArgs e)
        {
            this.vm.buttonComponents.ForEach(e => e.isChecked = false);
            (sender as ToggleButton).IsChecked = true;
        }

        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var buttonComponent = this.vm.buttonComponents.FirstOrDefault(f => f.isChecked);
            if (buttonComponent != null && buttonComponent.triggerTypeCoding == "3")
            {
                try
                {
                    ReadCardServer readCardServer = new ReadCardServer();
                    ReadCardResponse readCardResponse = readCardServer.readCard(new ReadCardRequest()
                    {
                        searchTypeCoding = buttonComponent.bizSearchTypeCoding,
                        searchValue = this.vm.searchText,
                        currBizTradeCode = this.vm.currBizTradeCode,
                        interfaceId = buttonComponent.interfaceId.ToString(),
                        uuheader = JsonConvert.SerializeObject(LoginInfo.loginUser),
                    });

                    this.vm.context.basicInfo = JsonConvert.DeserializeObject<BasicInfo>(JsonConvert.SerializeObject(readCardResponse));
                }
                catch (Exception ex)
                {
                    MessageBox.Show("读卡错误!" + ex.Message);
                    return;
                }
            }
        }
    }
}
